package cn.tedu;

public class Singleton {
    private Singleton() {
    }

    private static Singleton singleton = null;

    public static Singleton getSingleton() {
        if (singleton == null) { //判断是否需要锁住代码
            synchronized ("java") {
                if (singleton == null) { //判断是否创建对象
                    singleton = new Singleton();
                }
            }
        }
        return singleton;
    }
}

class Demo {
    public static void main(String[] args) {
        Singleton singleton = Singleton.getSingleton();
        System.out.println("singleton = " + singleton);
        Singleton singleton2 = Singleton.getSingleton();
        System.out.println("singleton2 = " + singleton2);
    }
}